package com.rt.cms.service.impl;

import com.rt.cms.dao.TbApprovalRecordMapper;
import com.rt.cms.dao.TbCompanyInfoMapper;
import com.rt.cms.data.entity.CompanyInfoBean;
import com.rt.cms.data.entity.TbApprovalRecord;
import com.rt.cms.data.entity.TbCompanyInfo;
import com.rt.cms.data.entity.TbCompayUploadInfo;
import com.rt.cms.data.requestParam.ArchivesParams;
import com.rt.cms.data.requestParam.CompanyParam;
import com.rt.cms.service.CompanyInfoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * 企业信息
 * Created by lgx on 2018/05/24.
 */
@Service
public class CompanyInfoServiceImpl implements CompanyInfoService {

    @Autowired
    private TbCompanyInfoMapper tbCompanyInfoMapper;
    @Autowired
    private TbApprovalRecordMapper tbApprovalRecordMapper;

    public List<TbCompanyInfo> getCompanyInfoByBusinessCode(String businessCode){
        return tbCompanyInfoMapper.getCompanyInfoByBusinessCode(businessCode);
    }

    //保存企业信息
    @Override
    @Transactional
    public int saveCompanyInfo(TbCompanyInfo tbCompayInfo, TbApprovalRecord tbApprovalRecord) {

        int companyId = tbCompayInfo.getId();
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        tbCompayInfo.setUuid(uuid);
        if (tbCompanyInfoMapper.insertCompanyInfo(tbCompayInfo) > 0) {
            tbApprovalRecord.setComInfoId(tbCompayInfo.getId());
            if (companyId > 0) {
                //企业和担保企业关联
                if (tbCompanyInfoMapper.insertCompanyRelate(companyId, tbCompayInfo.getId(), 0) < 0) {
                    throw new RuntimeException("插入关联信息失败");
                }
            }

            //记录操作记录
            if (tbApprovalRecordMapper.insertApprovalRecord(tbApprovalRecord) > 0) {
                return tbCompayInfo.getId();
            } else {
                throw new RuntimeException("插入信息失败");
            }
        }
        return 0;
    }

    //新增操作记录
    @Override
    public int saveApprovalRecord(TbApprovalRecord tbApprovalRecord) {

        //记录操作记录
        return tbApprovalRecordMapper.insertApprovalRecord(tbApprovalRecord);

    }


    //修改企业基本信息
    @Override
    public int editCompanyBasicInfo(TbCompanyInfo tbCompanyInfo, TbApprovalRecord tbApprovalRecord) {

        String uuid = UUID.randomUUID().toString().replace("-", "");
        tbCompanyInfo.setUuid(uuid);
        if (tbCompanyInfoMapper.updateCompanyBasicInfo(tbCompanyInfo) > 0) {
            tbApprovalRecord.setComInfoId(tbCompanyInfo.getId());
            //记录操作记录
            /*if (tbApprovalRecordMapper.insertApprovalRecord(tbApprovalRecord) > 0) {
                return 1;
            } else {
                throw new RuntimeException("插入信息失败");
            }*/
            return 1;
        }
        return 0;
    }

    //修改企业状态信息
    @Override
    public int editCompanyInfo(TbCompanyInfo tbCompanyInfo, TbApprovalRecord tbApprovalRecord) {

        String uuid = UUID.randomUUID().toString().replace("-", "");
        tbCompanyInfo.setUuid(uuid);
        if (tbCompanyInfoMapper.updateCompanyInfo(tbCompanyInfo) > 0) {
            tbApprovalRecord.setComInfoId(tbCompanyInfo.getId());
            //记录操作记录
            if (tbApprovalRecordMapper.insertApprovalRecord(tbApprovalRecord) > 0) {
                return 1;
            } else {
                throw new RuntimeException("插入信息失败");
            }
        }
        return 0;
    }

    //保存企业图片信息
    @Override
    public int saveCompanyUploadInfo(TbCompayUploadInfo tbCompayUploadInfo) {

        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        tbCompayUploadInfo.setUuid(uuid);
        return tbCompanyInfoMapper.insertCompanyUploadInfo(tbCompayUploadInfo);
    }

    //修改企业图片信息
    @Override
    public int editCompanyUploadInfo(TbCompayUploadInfo tbCompayUploadInfo) {

        String uuid = UUID.randomUUID().toString().replace("-", "");
        tbCompayUploadInfo.setUuid(uuid);
        return tbCompanyInfoMapper.updateCompanyUploadInfo(tbCompayUploadInfo);
    }

    //通过id查询企业信息
    public CompanyInfoBean getCompanyInfoBeanById(int id) {
        return tbCompanyInfoMapper.getCompayInfoBean(id);
    }

    public TbCompanyInfo getCompanyInfoById(int id) {
        return tbCompanyInfoMapper.getCompanyInfoById(id);
    }

    public TbCompayUploadInfo getCompanyUploadInfoById(int id) {
        return tbCompanyInfoMapper.getCompanyUploadInfoById(id);
    }


    public List<CompanyInfoBean> getGuaranteeCompanyInfoBean(int id) {
        return tbCompanyInfoMapper.getGuaranteeCompanyInfoBean(id);
    }

    public List<TbCompanyInfo> selectByPhpParam(ArchivesParams archivesParams){

        return tbCompanyInfoMapper.selectByPhpParam(archivesParams);
    }

    public int updateByPhpParam(ArchivesParams archivesParams){

        String uuid = UUID.randomUUID().toString().replace("-", "");

        return tbCompanyInfoMapper.updateByPhpParam(archivesParams,uuid);
    }

}
